package org.phalanx.client.menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;

import org.phalanx.client.PhalanxClient;
import org.phalanx.client.UI.DialogManager;
import org.phalanx.client.UI.PhalanxPanelManager;
import org.phalanx.client.dialogs.MainMenuPanel;
import org.phalanx.client.mapeditor.PhalanxMapEditorPanel;
import org.phalanx.client.mapeditor.dialogs.PhalanxNewMapDialog;
import org.phalanx.map.MapTileSet;
import org.phalanx.map.PhalanxMap;
import org.phalanx.map.PhalanxMapReader;
import org.phalanx.map.PhalanxMapWriter;

public class MapMenuAction implements ActionListener {
	private PhalanxMapEditorPanel masterPanel;
	
	public MapMenuAction(PhalanxMapEditorPanel masterPanel){
		this.masterPanel = masterPanel;
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JMenuItem){
			String actionCommand = ((JMenuItem)e.getSource()).getName();
			
			if (actionCommand.equalsIgnoreCase("gotomainmenu")){
				masterPanel.cleanUp();
				masterPanel.switchToMainMenu(false);
			}
			else if (actionCommand.equalsIgnoreCase("OpenMap")){
				openMap();
			}
			else if (actionCommand.equalsIgnoreCase("SaveMap")){
				saveMap(false);
			}
			else if (actionCommand.equalsIgnoreCase("newMap")) {
				newMap();
			}
			else if (actionCommand.equalsIgnoreCase("SaveMapAs")) {
				saveMap(true);
			}
			else if (actionCommand.equalsIgnoreCase("ExitEditor")) {
				masterPanel.cleanUp();
				masterPanel.switchToMainMenu(true);
			}
		}

	}
	
	private void newMap() {
		DialogManager d = DialogManager.getInstance();
		
		if (d.getDialog(PhalanxNewMapDialog.NAME) == null) {
			d.setDialog(PhalanxNewMapDialog.NAME, new PhalanxNewMapDialog(masterPanel));
		}
		d.getDialog(PhalanxNewMapDialog.NAME).setVisible(true);
	}
	
	private void openMap(){
		JFileChooser fc = new JFileChooser();
		fc.setCurrentDirectory(new File(masterPanel.getCurrentDirectory()));
		int fileOption = fc.showOpenDialog(this.masterPanel.getMasterFrame());

		if (fileOption == JFileChooser.APPROVE_OPTION){
			masterPanel.setCurrentDirectory(fc.getCurrentDirectory().getAbsolutePath());
			File fileName = fc.getSelectedFile();

			String cacheDir = PhalanxClient.getClientInstance().getPreferences().
			getProperties("PhalanxClient.PhalanxGame.Mapping.CacheDirectory", "./cache");

			PhalanxMapReader r = new PhalanxMapReader(PhalanxClient.getClientInstance().getDefaultTileSet(), cacheDir);
			r.setMapFile(fileName);
			if (r.readInMapData()){
				PhalanxMap newMap = r.getMap();
				masterPanel.getDisplayContainer().setMapObject(newMap);
				masterPanel.getDisplayContainer().setTitle(fileName.getAbsolutePath());
				masterPanel.setTileSetView(newMap.getMapTileSet());
				masterPanel.getDisplayContainer().getMapViewObject().setCoordinates(0, 0);
			}
		}
	}
	
	private void saveMap(boolean saveAs){
		PhalanxMap savedMap = masterPanel.getDisplayContainer().getMapViewObject().getMap();
		MapTileSet savedMapTileSet = savedMap.getMapTileSet();
		String editorVersion = PhalanxClient.getClientInstance().formattedVersion();
		boolean saveOk = false;
		
		boolean isDefaultTS = (savedMapTileSet != null && 
				savedMapTileSet.equals(PhalanxClient.getClientInstance().getDefaultTileSet()));
		String mapFileName = savedMap.getMapFilePath();
		
		if (mapFileName == null || saveAs) {
			JFileChooser fc = new JFileChooser();
			fc.setCurrentDirectory(new File(masterPanel.getCurrentDirectory()));
			int fileOption = fc.showSaveDialog(this.masterPanel.getMasterFrame());

			if (fileOption == JFileChooser.APPROVE_OPTION){
				mapFileName = fc.getSelectedFile().getAbsolutePath();
				saveOk = true;
				masterPanel.setCurrentDirectory(fc.getCurrentDirectory().getAbsolutePath());
			}
		}
		else {
			mapFileName = savedMap.getMapFilePath();
			saveOk = true;
		}
		
		if (mapFileName != null && saveOk) {
			savedMap.setMapFilePath(mapFileName);
			PhalanxMapWriter writer = new PhalanxMapWriter(savedMap, isDefaultTS);
			writer.setCreatedBy("Phalanx Map Editor v" + editorVersion);

			writer.writeMapPackageOut(mapFileName);
			masterPanel.getDisplayContainer().setTitle(mapFileName);
		}
	}

}
